{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "source": [
    "# iflearner client Configure and start"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Virtual environment\n",
    "You can create a virtual running environment of iflearner through virtual tools such as conda and pyenv, and then activate it. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1. Conda\n",
    "1. You need to install conda environment. [install](https://docs.conda.io/projects/conda/en/stable/user-guide/install/index.html)\n",
    "\n",
    "2. We will create a virtual environment through conda and activate it   \n",
    "```shell\n",
    "$ conda create -n iflearner python==3.9 ipykernel\n",
    "$ conda activate iflearner\n",
    "```\n",
    "\n",
    "3. Write the virtual environment into the kernel of jupyter notebook        \n",
    "command: python -m ipykernel install --user --name virtual-environment-name --display-name virtual-environment-name \n",
    "- The first virtual environment name indicates the created virtual environment name\n",
    "- The second virtual environment name indicates that you want it to appear in the kernel options of the jupyter noteboook\n",
    "\n",
    "  example: $ python -m ipykernel install --user --name iflearner --display-name \"iflearner\"\n",
    "\n",
    "4. switch kernel\n",
    "You can select the change kernel button in the kernel menu bar of jupyterlab and select the virtual environment we created"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Install the iflearner library and related dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: http://pypi.douban.com/simple\n",
      "Requirement already satisfied: torch==1.7.1 in /Users/lucky/opt/anaconda3/lib/python3.8/site-packages (1.7.1)\n",
      "Requirement already satisfied: torchvision==0.8.2 in /Users/lucky/opt/anaconda3/lib/python3.8/site-packages (0.8.2)\n",
      "Requirement already satisfied: typing-extensions in /Users/lucky/opt/anaconda3/lib/python3.8/site-packages (from torch==1.7.1) (4.3.0)\n",
      "Requirement already satisfied: numpy in /Users/lucky/opt/anaconda3/lib/python3.8/site-packages (from torch==1.7.1) (1.21.6)\n",
      "Requirement already satisfied: pillow>=4.1.1 in /Users/lucky/opt/anaconda3/lib/python3.8/site-packages (from torchvision==0.8.2) (9.2.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install iflearner torch==1.7.1 torchvision==0.8.2  --index-url http://pypi.douban.com/simple --trusted-host pypi.douban.com "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Define Client and Start"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1. Define Pytorch model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn.functional as F\n",
    "import torch.optim as optim\n",
    "from torch import nn\n",
    "from torchvision import datasets, transforms\n",
    "\n",
    "from iflearner.business.homo.argument import parser\n",
    "from iflearner.business.homo.pytorch_trainer import PyTorchTrainer\n",
    "from iflearner.business.homo.train_client import Controller\n",
    "\n",
    "\n",
    "class Model(nn.Module):\n",
    "    def __init__(self, num_channels, num_classes):\n",
    "        super().__init__()\n",
    "        self.conv1 = nn.Conv2d(num_channels, 10, kernel_size=5)\n",
    "        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)\n",
    "        self.conv2_drop = nn.Dropout2d()\n",
    "        self.fc1 = nn.Linear(320, 50)\n",
    "        self.fc2 = nn.Linear(50, num_classes)\n",
    "\n",
    "    def forward(self, x):\n",
    "        x = F.relu(F.max_pool2d(self.conv1(x), 2))\n",
    "        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))\n",
    "        x = x.view(-1, x.shape[1] * x.shape[2] * x.shape[3])\n",
    "        x = F.relu(self.fc1(x))\n",
    "        x = F.dropout(x, training=self.training)\n",
    "        x = self.fc2(x)\n",
    "        return F.log_softmax(x, dim=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2. Integrate and implement PytorchTrainer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Mnist(PyTorchTrainer):\n",
    "    def __init__(self, lr=0.15, momentum=0.5) -> None:\n",
    "        self._lr = lr\n",
    "        self._device = (\n",
    "            torch.device(\"cuda\") if torch.cuda.is_available() else torch.device(\"cpu\")\n",
    "        )\n",
    "        print(f\"device: {self._device}\")\n",
    "        self._model = Model(num_channels=1, num_classes=10).to(self._device)\n",
    "\n",
    "        super().__init__(self._model)\n",
    "\n",
    "        self._optimizer = optim.SGD(self._model.parameters(), lr=lr, momentum=momentum)\n",
    "        self._loss = F.nll_loss\n",
    "\n",
    "        apply_transform = transforms.Compose(\n",
    "            [transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]\n",
    "        )\n",
    "        train_dataset = datasets.MNIST(\n",
    "            \"./data\", train=True, download=True, transform=apply_transform\n",
    "        )\n",
    "        test_dataset = datasets.MNIST(\n",
    "            \"./data\", train=False, download=True, transform=apply_transform\n",
    "        )\n",
    "        self._train_data = torch.utils.data.DataLoader(\n",
    "            train_dataset, batch_size=64, shuffle=True\n",
    "        )\n",
    "        self._test_data = torch.utils.data.DataLoader(\n",
    "            test_dataset, batch_size=64, shuffle=False\n",
    "        )\n",
    "\n",
    "    def fit(self, epoch):\n",
    "        self._model.to(self._device)\n",
    "        self._model.train()\n",
    "        print(\n",
    "            f\"Epoch: {epoch}, the size of training dataset: {len(self._train_data.dataset)}, batch size: {len(self._train_data)}\"\n",
    "        )\n",
    "        for batch_idx, (data, target) in enumerate(self._train_data):\n",
    "            data, target = data.to(self._device), target.to(self._device)\n",
    "            self._optimizer.zero_grad()\n",
    "            output = self._model(data)\n",
    "            loss = self._loss(output, target)\n",
    "            loss.backward()\n",
    "            self._optimizer.step()\n",
    "\n",
    "    def evaluate(self, epoch):\n",
    "        self._model.to(self._device)\n",
    "        self._model.eval()\n",
    "        test_loss = 0\n",
    "        correct = 0\n",
    "        print(f\"The size of testing dataset: {len(self._test_data.dataset)}\")\n",
    "        with torch.no_grad():\n",
    "            for data, target in self._test_data:\n",
    "                data, target = data.to(self._device), target.to(self._device)\n",
    "                output = self._model(data)\n",
    "                test_loss += self._loss(\n",
    "                    output, target, reduction=\"sum\"\n",
    "                ).item()  # sum up batch loss\n",
    "                pred = output.argmax(\n",
    "                    dim=1, keepdim=True\n",
    "                )  # get the index of the max log-probability\n",
    "                correct += pred.eq(target.view_as(pred)).sum().item()\n",
    "        test_loss /= len(self._test_data.dataset)\n",
    "\n",
    "        print(\n",
    "            \"Test set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\".format(\n",
    "                test_loss,\n",
    "                correct,\n",
    "                len(self._test_data.dataset),\n",
    "                100.0 * correct / len(self._test_data.dataset),\n",
    "            )\n",
    "        )\n",
    "\n",
    "        return {\"loss\": test_loss, \"acc\": correct / len(self._test_data.dataset)}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3. Start client"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:10:04.701 | INFO     | iflearner.business.homo.train_client:run:89 - register to server\n",
      "2022-08-08 15:10:04.760 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_register, time: 56.733226000000414ms\n",
      "2022-08-08 15:10:04.761 | INFO     | iflearner.business.homo.train_client:run:106 - use strategy: FedAvg\n",
      "2022-08-08 15:10:04.763 | INFO     | iflearner.business.homo.train_client:run:139 - report client ready\n",
      "2022-08-08 15:10:04.772 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_client_ready, time: 6.8441579999998226ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Namespace(cert=None, enable_ll=0, epochs=10, name='client', peers=None, server='localhost:50001')\n",
      "device: cpu\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:10:21.889 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_notify_training, time: 2.2349760000004437ms\n",
      "2022-08-08 15:10:22.829 | INFO     | iflearner.business.homo.train_client:run:149 - ----- fit <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 1, the size of training dataset: 60000, batch size: 938\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:10:49.362 | INFO     | iflearner.business.homo.train_client:run:167 - ----- evaluate <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The size of testing dataset: 10000\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:10:51.863 | INFO     | iflearner.business.homo.train_client:run:178 - ----- get <FT> -----\n",
      "2022-08-08 15:10:51.884 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_upload_param, time: 17.990439999998387ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test set: Average loss: 0.1211, Accuracy: 9666/10000 (96.66%)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:10:52.080 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_aggregate_result, time: 15.534564999995837ms\n",
      "2022-08-08 15:10:52.892 | INFO     | iflearner.business.homo.train_client:run:221 - ----- set -----\n",
      "2022-08-08 15:10:52.901 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_client_ready, time: 6.33272800000384ms\n",
      "2022-08-08 15:10:54.093 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_notify_training, time: 2.4183210000003896ms\n",
      "2022-08-08 15:10:54.907 | INFO     | iflearner.business.homo.train_client:run:149 - ----- fit <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 2, the size of training dataset: 60000, batch size: 938\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:11:22.692 | INFO     | iflearner.business.homo.train_client:run:167 - ----- evaluate <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The size of testing dataset: 10000\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:11:25.896 | INFO     | iflearner.business.homo.train_client:run:178 - ----- get <FT> -----\n",
      "2022-08-08 15:11:25.915 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_upload_param, time: 15.455973999991102ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test set: Average loss: 0.1083, Accuracy: 9662/10000 (96.62%)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:11:26.288 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_aggregate_result, time: 11.943896000005338ms\n",
      "2022-08-08 15:11:26.919 | INFO     | iflearner.business.homo.train_client:run:221 - ----- set -----\n",
      "2022-08-08 15:11:26.926 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_client_ready, time: 4.7207749999955695ms\n",
      "2022-08-08 15:11:27.293 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_notify_training, time: 1.8890160000069045ms\n",
      "2022-08-08 15:11:27.930 | INFO     | iflearner.business.homo.train_client:run:149 - ----- fit <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 3, the size of training dataset: 60000, batch size: 938\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:11:55.560 | INFO     | iflearner.business.homo.train_client:run:167 - ----- evaluate <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The size of testing dataset: 10000\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:11:59.345 | INFO     | iflearner.business.homo.train_client:run:178 - ----- get <FT> -----\n",
      "2022-08-08 15:11:59.384 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_upload_param, time: 23.15518700000041ms\n",
      "2022-08-08 15:11:59.540 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_aggregate_result, time: 12.790113000008319ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test set: Average loss: 0.0712, Accuracy: 9781/10000 (97.81%)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:12:00.393 | INFO     | iflearner.business.homo.train_client:run:221 - ----- set -----\n",
      "2022-08-08 15:12:00.411 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_client_ready, time: 11.972892999992268ms\n",
      "2022-08-08 15:12:00.548 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_notify_training, time: 3.832819999999515ms\n",
      "2022-08-08 15:12:01.417 | INFO     | iflearner.business.homo.train_client:run:149 - ----- fit <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 4, the size of training dataset: 60000, batch size: 938\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:12:31.813 | INFO     | iflearner.business.homo.train_client:run:167 - ----- evaluate <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The size of testing dataset: 10000\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:12:35.402 | INFO     | iflearner.business.homo.train_client:run:178 - ----- get <FT> -----\n",
      "2022-08-08 15:12:35.421 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_upload_param, time: 14.573259999991706ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test set: Average loss: 0.0716, Accuracy: 9786/10000 (97.86%)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:12:35.780 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_aggregate_result, time: 16.237942999993038ms\n",
      "2022-08-08 15:12:36.426 | INFO     | iflearner.business.homo.train_client:run:221 - ----- set -----\n",
      "2022-08-08 15:12:36.433 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_client_ready, time: 4.858716000001095ms\n",
      "2022-08-08 15:12:36.789 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_notify_training, time: 3.7139959999876737ms\n",
      "2022-08-08 15:12:37.438 | INFO     | iflearner.business.homo.train_client:run:149 - ----- fit <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 5, the size of training dataset: 60000, batch size: 938\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:13:01.699 | INFO     | iflearner.business.homo.train_client:run:167 - ----- evaluate <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The size of testing dataset: 10000\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:13:04.071 | INFO     | iflearner.business.homo.train_client:run:178 - ----- get <FT> -----\n",
      "2022-08-08 15:13:04.089 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_upload_param, time: 15.177445000006173ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test set: Average loss: 0.0654, Accuracy: 9806/10000 (98.06%)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:13:04.970 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_aggregate_result, time: 16.106734000004508ms\n",
      "2022-08-08 15:13:05.093 | INFO     | iflearner.business.homo.train_client:run:221 - ----- set -----\n",
      "2022-08-08 15:13:05.099 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_client_ready, time: 4.25795600000356ms\n",
      "2022-08-08 15:13:05.975 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_notify_training, time: 2.572984000011047ms\n",
      "2022-08-08 15:13:06.104 | INFO     | iflearner.business.homo.train_client:run:149 - ----- fit <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 6, the size of training dataset: 60000, batch size: 938\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:13:32.638 | INFO     | iflearner.business.homo.train_client:run:167 - ----- evaluate <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The size of testing dataset: 10000\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:13:35.528 | INFO     | iflearner.business.homo.train_client:run:178 - ----- get <FT> -----\n",
      "2022-08-08 15:13:35.563 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_upload_param, time: 27.60979300001054ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test set: Average loss: 0.0661, Accuracy: 9793/10000 (97.93%)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:13:36.181 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_aggregate_result, time: 10.007220000005645ms\n",
      "2022-08-08 15:13:36.570 | INFO     | iflearner.business.homo.train_client:run:221 - ----- set -----\n",
      "2022-08-08 15:13:36.576 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_client_ready, time: 3.995329999980868ms\n",
      "2022-08-08 15:13:38.195 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_notify_training, time: 2.187747000022ms\n",
      "2022-08-08 15:13:38.584 | INFO     | iflearner.business.homo.train_client:run:149 - ----- fit <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 7, the size of training dataset: 60000, batch size: 938\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:14:02.057 | INFO     | iflearner.business.homo.train_client:run:167 - ----- evaluate <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The size of testing dataset: 10000\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:14:04.349 | INFO     | iflearner.business.homo.train_client:run:178 - ----- get <FT> -----\n",
      "2022-08-08 15:14:04.367 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_upload_param, time: 14.917258999986416ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test set: Average loss: 0.0635, Accuracy: 9819/10000 (98.19%)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:14:05.348 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_aggregate_result, time: 10.620224000007283ms\n",
      "2022-08-08 15:14:05.370 | INFO     | iflearner.business.homo.train_client:run:221 - ----- set -----\n",
      "2022-08-08 15:14:05.381 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_client_ready, time: 8.581340000006321ms\n",
      "2022-08-08 15:14:06.356 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_notify_training, time: 1.6289750000169079ms\n",
      "2022-08-08 15:14:06.395 | INFO     | iflearner.business.homo.train_client:run:149 - ----- fit <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 8, the size of training dataset: 60000, batch size: 938\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:14:28.942 | INFO     | iflearner.business.homo.train_client:run:167 - ----- evaluate <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The size of testing dataset: 10000\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:14:31.183 | INFO     | iflearner.business.homo.train_client:run:178 - ----- get <FT> -----\n",
      "2022-08-08 15:14:31.202 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_upload_param, time: 16.205119999995077ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test set: Average loss: 0.0596, Accuracy: 9814/10000 (98.14%)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:14:31.518 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_aggregate_result, time: 14.15860600002361ms\n",
      "2022-08-08 15:14:32.208 | INFO     | iflearner.business.homo.train_client:run:221 - ----- set -----\n",
      "2022-08-08 15:14:32.217 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_client_ready, time: 5.9485140000106185ms\n",
      "2022-08-08 15:14:33.532 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_notify_training, time: 2.3720880000155375ms\n",
      "2022-08-08 15:14:34.226 | INFO     | iflearner.business.homo.train_client:run:149 - ----- fit <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 9, the size of training dataset: 60000, batch size: 938\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:14:57.795 | INFO     | iflearner.business.homo.train_client:run:167 - ----- evaluate <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The size of testing dataset: 10000\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:15:00.103 | INFO     | iflearner.business.homo.train_client:run:178 - ----- get <FT> -----\n",
      "2022-08-08 15:15:00.121 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_upload_param, time: 15.236562999973557ms\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test set: Average loss: 0.0696, Accuracy: 9803/10000 (98.03%)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:15:00.673 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_aggregate_result, time: 10.87519500003964ms\n",
      "2022-08-08 15:15:01.127 | INFO     | iflearner.business.homo.train_client:run:221 - ----- set -----\n",
      "2022-08-08 15:15:01.134 | INFO     | iflearner.communication.homo.homo_client:transport:59 - OUT: message type: msg_client_ready, time: 5.139049000035811ms\n",
      "2022-08-08 15:15:01.681 | INFO     | iflearner.communication.homo.homo_client:notice:94 - IN: party: message type: msg_notify_training, time: 3.1764629999884164ms\n",
      "2022-08-08 15:15:02.137 | INFO     | iflearner.business.homo.train_client:run:149 - ----- fit <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 10, the size of training dataset: 60000, batch size: 938\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:15:24.819 | INFO     | iflearner.business.homo.train_client:run:167 - ----- evaluate <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The size of testing dataset: 10000\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-08-08 15:15:27.169 | INFO     | iflearner.business.homo.train_client:run:178 - ----- get <FT> -----\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test set: Average loss: 0.0638, Accuracy: 9824/10000 (98.24%)\n",
      "label: FT, points: ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0.12112331417798997, 0.10830172061696648, 0.07118311848919838, 0.07162178324311971, 0.06537593331132084, 0.0661331262275693, 0.06346169429037254, 0.05963607459508348, 0.06964817571002059, 0.06378729571889272])\n",
      "label: LT, points: ([1], [0.12112331417798997])\n",
      "label: FT, points: ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0.9666, 0.9662, 0.9781, 0.9786, 0.9806, 0.9793, 0.9819, 0.9814, 0.9803, 0.9824])\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    args = parser.parse_args(args=[])\n",
    "    print(args)\n",
    "    args.name = \"client01\"\n",
    "    args.epochs = 2\n",
    "    mnist = Mnist()\n",
    "    controller = Controller(args, mnist)\n",
    "    controller.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "iflearner",
   "language": "python",
   "name": "iflearner"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
